package cmj.cloudmusic.activity;

import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import java.io.File;
import java.io.IOException;

import cmj.cloudmusic.R;

public class VideoRecordActivity extends AppCompatActivity implements View.OnClickListener{

    private Button start,stop;
    private SurfaceView surface;
    private MediaRecorder recorder;
    private boolean isRecording;//录制的状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record);
        initViews();
        initRecorder();
    }

    private void initViews() {
        start=(Button)findViewById(R.id.start);
        stop=(Button)findViewById(R.id.stop);
        surface=(SurfaceView)findViewById(R.id.surface);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);

        //横屏显示
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置半透明
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        //设置surfaceview
        //设置分辨率
        surface.getHolder().setFixedSize(1280,720);
        //禁止屏幕自动关闭
        surface.getHolder().setKeepScreenOn(true);
    }

    /**
     * 创建对象
     */
    private void initRecorder() {
        recorder=new MediaRecorder();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                start.setEnabled(false);
                stop.setEnabled(true);
                startRecording();
                break;
            case R.id.stop:
                start.setEnabled(true);
                stop.setEnabled(false);
                stopRecording();
                break;
        }
    }

    /**
     * 开始录制
     */
    private void startRecording(){
        //初始化
        //使用麦克风录制
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //使用摄像头录制视频
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        //设置视频的录制参数
        //设置输出视频的格式为MP4
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        //设置音频编码
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        //设置视频编码
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        //设置录入的视频大小
        recorder.setVideoSize(176,144);
        //设置视频帧大小
        recorder.setVideoFrameRate(20);
        //设置视频存放的路径
        try {
            File file=new File(Environment.getExternalStorageDirectory().getCanonicalPath(),"myVideo.mp4");
            recorder.setOutputFile(file.getAbsolutePath());
            //预览视频效果
            recorder.setPreviewDisplay(surface.getHolder().getSurface());

            //准备录制
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //开始录制
        recorder.start();
        //更改录制状态
        isRecording=true;
    }

    /**
     * 停止录制
     */
    private void stopRecording(){
        //停止
        recorder.stop();
        //重置
        recorder.reset();
        //释放资源
        recorder.release();
        //更改录制状态
        isRecording=false;
    }

    @Override
    protected void onDestroy() {
        if(isRecording) {
            stopRecording();
        }
        //对象置空
        recorder=null;
        super.onDestroy();
    }
}
